package pers.xiaojun.boot.module.system.controller.tenant;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.security.PermitAll;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import pers.xiaojun.boot.common.pojo.CommonResult;
import pers.xiaojun.boot.common.pojo.PageResult;
import pers.xiaojun.boot.common.util.BeanUtils;
import pers.xiaojun.boot.module.system.controller.tenant.vo.tenant.TenantQueryReqVO;
import pers.xiaojun.boot.module.system.controller.tenant.vo.tenant.TenantRespVO;
import pers.xiaojun.boot.module.system.controller.tenant.vo.tenant.TenantSaveReqVO;
import pers.xiaojun.boot.module.system.controller.tenant.vo.tenant.TenantSimpleRespVO;
import pers.xiaojun.boot.module.system.service.tenant.TenantService;
import pers.xiaojun.boot.mybatis.core.convert.PageConvert;
import pers.xiaojun.boot.tenant.core.annotation.TenantIgnore;
import pers.xiaojun.boot.web.core.annotation.AdminApi;

import java.util.List;

/**
 * 租户接口
 *
 * @author xiaojun
 * @since 2025-11-22
 */
@Tag(name = "租户")
@AdminApi
@RestController
@RequestMapping("/system/tenant")
@RequiredArgsConstructor
public class TenantController {
    
    private final TenantService tenantService;

    @PostMapping
    @PreAuthorize("@ss.hasPermission('system:tenant:create')")
    @Operation(summary = "创建租户")
    public CommonResult<?> createTenant(@RequestBody @Valid TenantSaveReqVO createVO) {
        return CommonResult.success(tenantService.saveTenant(createVO).getId());
    }

    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:tenant:update')")
    @Operation(summary = "更新租户")
    @Parameter(name = "id", description = "租户id", required = true)
    public CommonResult<?> updateTenant(@PathVariable("id") Long tenantId, @RequestBody @Valid TenantSaveReqVO updateVO) {
        updateVO.setId(tenantId);
        return CommonResult.success(tenantService.updateTenant(updateVO).getId());
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:tenant:delete')")
    @Operation(summary = "删除租户")
    @Parameter(name = "id", description = "租户id", required = true)
    public CommonResult<?> deleteTenant(@PathVariable("id") Long tenantId) {
        tenantService.removeTenantById(tenantId);
        return CommonResult.success();
    }

    @DeleteMapping("/batch/{ids}")
    @PreAuthorize("@ss.hasPermission('system:tenant:delete')")
    @Operation(summary = "删除一批租户")
    @Parameter(name = "ids", description = "租户id列表（使用,分隔）", required = true)
    public CommonResult<?> deleteTenantBatch(@PathVariable("ids") List<Long> tenantIds) {
        tenantService.removeTenantByIds(tenantIds);
        return CommonResult.success();
    }

    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('system:tenant:query')")
    @Operation(summary = "根据租户Id获取租户信息")
    @Parameter(name = "id", description = "租户id", required = true)
    public CommonResult<TenantRespVO> getTenantById(@PathVariable("id") Long tenantId) {
        return CommonResult.success(BeanUtil.toBean(tenantService.getTenantById(tenantId), TenantRespVO.class));
    }

    @GetMapping("/simple/by-name")
    @PermitAll
    @TenantIgnore
    @Operation(summary = "根据租户名称获取简要租户信息")
    @Parameter(name = "name", description = "租户名称", required = true)
    public CommonResult<TenantSimpleRespVO> getSimpleTenantByName(@RequestParam("name") String tenantName) {
        return CommonResult.success(BeanUtil.toBean(tenantService.getTenantByName(tenantName), TenantSimpleRespVO.class));
    }

    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('system:tenant:query')")
    @Operation(summary = "获取租户分页")
    public CommonResult<PageResult<TenantRespVO>> getTenantList(@Valid TenantQueryReqVO queryVO) {
        return CommonResult.success(PageConvert.INSTANCE.toPageResult(tenantService.listTenantsPage(queryVO), TenantRespVO.class));
    }

    @GetMapping("/simple-list")
    @PreAuthorize("@ss.hasPermission('system:tenant:query')")
    @Operation(summary = "获取简要租户列表")
    public CommonResult<List<TenantSimpleRespVO>> getSimpleTenantList() {
        return CommonResult.success(BeanUtils.toBeans(tenantService.list(), TenantSimpleRespVO.class));
    }


}
